package com.anquing.ssodemo.controller;

import com.alibaba.fastjson.JSON;
import com.anquing.ssodemo.common.Const;
import com.anquing.ssodemo.entity.CodeBean;
import com.anquing.ssodemo.redisService.RedisService;
import com.anquing.ssodemo.util.CodecUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping
public class userController {

    Logger log = LoggerFactory.getLogger(this.getClass());

    @PostMapping("/login")
    public String login(HttpServletRequest request,String username,String password,String returnUrl,String appName,RedirectAttributes attr){

        if(Const.USERNAME.equals(username)&&Const.PASSWORD.equals(password)){

            //登录成功
            // 1、生成token，设置session
            String token = CodecUtil.createUUID();
            HttpSession session = request.getSession();
            String id = session.getId();
            log.info("/login，SessionId="+id);
            session.setAttribute("isLogin", true);
            session.setAttribute("token",token);

            // 2、将应用名注册到认证中心
            List<String> appNameList = new ArrayList<>();
            appNameList.add(appName);
            String appNameStr = JSON.toJSONString(appNameList);
            RedisService.setex(Const.TOKEN_PREFIX+token, appNameStr, Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
            attr.addAttribute("token", token);

            return "redirect:"+returnUrl;
        }else{
            attr.addAttribute("returnUrl", returnUrl);
            attr.addAttribute("appName", appName);
            return "redirect:/login";
        }
    }

    @GetMapping("/login")
    public String loginPage(HttpServletRequest request,Model model,RedirectAttributes attr){

        String returnUrl = request.getParameter("returnUrl");
        String appName = request.getParameter("appName");

       HttpSession session = request.getSession();
        String id = session.getId();
        log.info("/login，SessionId="+id);
        Boolean isLogin = (Boolean)session.getAttribute("isLogin");
        String token = (String)session.getAttribute("token");
        if(isLogin!=null&&isLogin){
            //返回token
            attr.addAttribute("token",token);
            return "redirect:"+returnUrl;
        }
        model.addAttribute("returnUrl",returnUrl);
        model.addAttribute("appName",appName);
        return "login";
    }

    @PostMapping("/verified")
    @ResponseBody
    public Object verified(HttpServletRequest request){

        String token = request.getParameter("token");
        String appName = request.getParameter("appName");

        //验证
        String appNameStr = RedisService.get(Const.TOKEN_PREFIX + token);
        if(appNameStr!=null){
            List<String> appNameList = JSON.parseArray(appNameStr, String.class);
            if(!appNameList.contains(appName)){
                appNameList.add(appName);
            }
            appNameStr = JSON.toJSONString(appNameList);
            RedisService.setex(Const.TOKEN_PREFIX+token, appNameStr, Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
            return "0000";
        }else{
            return "0001";
        }

    }


}
